<?php
	class District extends MY_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('country_model');
			$this->load->model('city_model');
			$this->load->model('district_model');
		}
		
		public function index($limit=FALSE){
			$this->load->helper("form");
			//set the limit or offset for each page
			$this->district_model->pager($limit);
			$my_link = "";
			
			// define the link for pagination
			$my_link = site_url('district');				
		
			$data['district'] = $this->district_model->get_district();
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->district_model->get_total(), $this->district_model->get_record_per_page());
			
			$data['city'] = $this->get_country_city();
			
			$data['title'] = "District list";
			$this->load->view('templates/header1', $data);
			$this->load->view('district/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function filter($c_id=FALSE, $limit=FALSE){
			$this->load->helper("form");
			//set the limit or offset for each page
			$this->district_model->pager($limit);
				
			// define the link for pagination
			$my_link = site_url('district/filter/'.$c_id);				
		
			$data['district'] = $this->district_model->get_district(FALSE, $c_id);
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->district_model->get_total_filter($c_id), $this->district_model->get_record_per_page(), 4);
			
			$data['city'] = $this->get_country_city();
			
			$data['c_id'] = $c_id;
			$data['title'] = "District list";
			$this->load->view('templates/header1', $data);
			$this->load->view('district/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function search($src=FALSE,$limit=FALSE){
			$this->load->helper("form");
			$data['title'] = "District list";
			$search_text = '';
			
			//set the limit or offset for each page
			$this->district_model->pager($limit);			
			
			if($src!=FALSE){				
				$data['district'] = $this->district_model->search($src);		
				$search_text = $src;
			}
			else{
				$search_text = $this->input->post('search_text');				
				$data['district'] = $this->district_model->search($search_text);		
			}
			
			// define the link for pagination
			$my_link = site_url('district/search/'.$search_text);
			
			// call the pagination from parent controller get_total_search
			$this->pager($my_link, $this->district_model->get_total_search($search_text), $this->district_model->get_record_per_page(), 4);
			
			$data['city'] = $this->get_country_city();
			
			$this->load->view('templates/header1', $data);
			$this->load->view('district/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function displayForm($id=FALSE){			
				$this->load->helper("form");
				$data = array();
				if($id!=FALSE){
					$data['district'] = $this->district_model->get_district($id, FALSE);				
				}				
				$data['title']="Create District";				
				
				$data['city'] = $this->get_country_city();				
				
				$this->load->view('templates/header1', $data);
				$this->load->view('district/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			
		}
		
		public function create(){
			$this->displayForm();
		}
		
		public function edit($id){
			$this->displayForm($id);
		}
		
		public function delete($id){
			$this->district_model->delete($id);
			redirect("district", "refresh");
		}
		
		public function save(){			
			
			$this->load->helper("form");
			$this->load->library('form_validation');
			$this->form_validation->set_rules('name', 'Name', 'required');
			
			if($this->form_validation->run() === FALSE){
				$data['city'] = $this->get_country_city();	
				$id = $this->input->post('id');
				if(!empty($id))
					$data['district'] = $this->district_model->get_district($id);	
				$data['title']="Create District";
				$this->load->view('templates/header1', $data);
				$this->load->view('district/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$this->district_model->save();
				redirect("district", "refresh");
			}			
		}	
		
		public function get_country_city(){
			$arr = $this->country_model->get_country_list();
			if(count($arr)){
				foreach($arr as &$v){
					$v['city_data'] = $this->city_model->get_city_list($v['iso']);
				}
			}
			return $arr;
		}
		
	}
?>